
'''
创建文章表
CREATE TABLE `hw_article` (
  `id` varchar(64) COLLATE utf8mb4_bin NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `content` mediumtext COLLATE utf8mb4_bin,
  `add_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `type` int(11) DEFAULT NULL,
  `year` int(11) DEFAULT NULL,
  `origin_content` mediumtext COLLATE utf8mb4_bin,
  `realease_date` date DEFAULT NULL,
  `category` int(11) DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
'''
from models.database import db

class Article(db.Model):
    __tablename__ = 'hw_article'
    id = db.Column(db.String(64), primary_key=True)
    title = db.Column(db.String(255), nullable=True)
    content = db.Column(db.Text, nullable=True)
    add_time = db.Column(db.DateTime, nullable=True)
    update_time = db.Column(db.DateTime, nullable=True)
    type = db.Column(db.Integer, nullable=True)
    year = db.Column(db.Integer, nullable=True)
    origin_content = db.Column(db.Text, nullable=True)
    realease_date = db.Column(db.Date, nullable=True)
    category = db.Column(db.Integer, nullable=True)
    
    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'add_time': self.add_time.strftime('%Y-%m-%d %H:%M:%S') if self.add_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
            'type': self.type,
            'year': self.year,
            'origin_content': self.origin_content,
            'realease_date': self.realease_date.strftime('%Y-%m-%d') if self.realease_date else None,
            'category': self.category,
        }

